package com.ledim.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.utils.DensityUtil;
import com.ledim.utils.FrescoUtil;
import com.letv.android.young.client.R;
import java.util.ArrayList;

public class EpisodesAdapter extends LedimBaseListAdapter<LedimEpisodeBean> {

    private int mSelectedPosition = -1;
    private int mWidth;
    private int mHeight;

    public EpisodesAdapter(Context context, ArrayList<LedimEpisodeBean> list) {
        super(context);
        setDatas(list);
        mInflater = LayoutInflater.from(context);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mWidth = (int) ((wm.getDefaultDisplay().getWidth() - DensityUtil.dip2px(context, 10)) /
                2.5);
        mHeight = mWidth * 70 / 125;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_album_listview, null);
            holder.mImage = (SimpleDraweeView) convertView.findViewById(R.id.item_album_image);
            holder.mName = (TextView) convertView.findViewById(R.id.item_album_name);
            holder.mEpisode = (TextView) convertView.findViewById(R.id.item_album_episode);
            holder.mBg = convertView.findViewById(R.id.content_bg);
            ViewGroup.LayoutParams params = holder.mImage.getLayoutParams();
            params.height = mHeight;
            params.width = mWidth;
            holder.mImage.setLayoutParams(params);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final LedimEpisodeBean episode = getItem(position);
        //        //是正片部分显示，非正片全部显示
        //        if (pIsPositive){
        //            if (!episode.videoType.equals("{}")){
        //                switch (Integer.parseInt(Utils.StringToKey(episode.videoType))) {
        //                    case POSITIVE://正片
        //                    case TV:
        //                        holder.mBg.setVisibility(View.VISIBLE);
        //                        break;
        //                    default:
        //                        holder.mBg.setVisibility(View.GONE);
        //                        break;
        //                }
        //            }
        //        } else {
        //            holder.mBg.setVisibility(View.VISIBLE);
        //        }
        holder.mBg.setVisibility(View.VISIBLE);
        String picPath = episode.getPic();
        if (!TextUtils.isEmpty(picPath)) {
            FrescoUtil.loadUrl(picPath, holder.mImage);
        }
        if (episode.nameCn != null && episode.nameCn.length() > 0) {
            holder.mName.setVisibility(View.VISIBLE);
            holder.mName.setText(episode.nameCn);
        } else if (episode.subTitle != null && episode.subTitle.length() > 0) {
            holder.mName.setVisibility(View.VISIBLE);
            holder.mName.setText(episode.subTitle);
        } else {
            holder.mName.setVisibility(View.GONE);
        }
        if (episode.episode != null && episode.episode.length() > 0) {
            holder.mEpisode.setVisibility(View.VISIBLE);
            if (episode.episode.length() == 8) {
                String str = episode.episode + "";
                holder.mEpisode.setText(str.substring(0, 4) + "-" + str.substring(4, 6) + "-" +
                        str.substring(6, str.length()) + "期");
            } else {
                holder.mEpisode.setText(episode.episode + "期");
            }
        } else {
            holder.mEpisode.setVisibility(View.GONE);
        }
        if (mSelectedPosition == position) {
            holder.mName.setTextColor(Color.parseColor("#1a1a1a"));
            holder.mBg.setBackgroundColor(Color.parseColor("#FEFBBD"));
        } else {
            holder.mName.setTextColor(Color.parseColor("#333333"));
            holder.mBg.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        return convertView;
    }

    class ViewHolder {

        private SimpleDraweeView mImage;
        private TextView mName;
        private TextView mEpisode;
        private View mBg;
    }

    //listview选中的item
    public void setSelectItem(int position) {
        this.mSelectedPosition = position;
    }
}
